package com.xju.controller;

import com.xju.bean.JdCategory;
import com.xju.bean.JdUser;
import com.xju.service.JdCategoryService;
import com.xju.service.JdUserService;
import com.xju.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class JdUserController {
    @Autowired
    private JdUserService userService;
    @Autowired
    private JdCategoryService jdCategoryService;
    @RequestMapping("/toHello")
    public String toHello(){
        return "hello";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 功能描述
     * @author ctx
        * @param login_name 账号
        * @param password 密码
     * */
    @RequestMapping("login.do")
    public String login(String login_name, String password, HttpServletRequest request){
        password = MD5Util.md5StrAndSalt(password,login_name);
        JdUser jdUser = userService.login(login_name,password);
        if(jdUser!=null){
            request.getSession().setAttribute("jdUser",jdUser);
            List<JdCategory> jdCategories = jdCategoryService.subCategoryList(1);
            request.setAttribute("jdCategories",jdCategories);
            return "book";
        }
        request.setAttribute("msg","登陆失败");
        return "login";
    }
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("register.do")
    public String register(JdUser jdUser,HttpServletRequest request){
        jdUser.setPassword(MD5Util.md5StrAndSalt(jdUser.getPassword(),jdUser.getLogin_name()));
        boolean rf = userService.register(jdUser);
        if(rf){
            return "registerOk";
        }
        request.setAttribute("msg","注册失败");
        return "register";
    }

}
